local extension = Package("freak_alliance_constellation")
extension.extensionName = "freak_alliance"

Fk:loadTranslationTable{
  ["freak_alliance"] = "怀异集", 
  ["freak_alliance_constellation"] = "怀异集·星象",
  ["fa"] = "怀异",
}

local fa__xuchu = General(extension, "fa__xuchu", "wei", 4,4)

local fa__xiechan = fk.CreateTriggerSkill{
  name = "fa__xiechan",
  anim_type = "control",
  events = {fk.TargetSpecified, fk.TargetConfirmed},
  can_trigger = function(self, event, target, player, data)
    local from = player.room:getPlayerById(data.from)
    local to = player.room:getPlayerById(data.to)
    if event == fk.TargetSpecified then
      if not (target == player and player:hasSkill(self) and data.card.trueName == 'slash') then return end
      return player.id ~= to 
else
    if event == fk.TargetConfirmed then
      if not (target == player and player:hasSkill(self) and data.card.trueName == 'slash') then return end
      return player.id ~= from 
    end
  end
  end,
  on_use = function(self, event, target, player, data)
    if event == fk.TargetSpecified then
    local room = player.room
    local from = room:getPlayerById(data.from)
    local to = room:getPlayerById(data.to)
    if player == from then
    if to:isNude() then return end
    room:notifySkillInvoked(player, self.name)
    player:showCards(player:getCardIds("h"))
    local cards1 = table.filter(player:getCardIds("h"), function(id) return Fk:getCardById(id).trueName == "jink" end)
    room:moveCardTo(cards1, Card.DiscardPile, player, fk.ReasonDiscard, self.name, nil, false, player.id)
    to:showCards(to:getCardIds("h"))
    local cards2 = table.filter(to:getCardIds("h"), function(id) return Fk:getCardById(id).trueName == "jink" end)
    room:moveCardTo(cards2, Card.DiscardPile, to, fk.ReasonDiscard, self.name, nil, false, player.id)
    end
 else
    if event == fk.TargetConfirmed then
      local room = player.room
      local from = room:getPlayerById(data.from)
      local to = room:getPlayerById(data.to)
      if player == to then
      if from:isNude() then return end
      room:notifySkillInvoked(player, self.name)
      player:showCards(player:getCardIds("h"))
      local cards3 = table.filter(player:getCardIds("h"), function(id) return Fk:getCardById(id).trueName == "jink" end)
      room:moveCardTo(cards3, Card.DiscardPile, player, fk.ReasonDiscard, self.name, nil, false, player.id)
      from:showCards(from:getCardIds("h"))
      local cards4 = table.filter(from:getCardIds("h"), function(id) return Fk:getCardById(id).trueName == "jink" end)
      room:moveCardTo(cards4, Card.DiscardPile, from, fk.ReasonDiscard, self.name, nil, false, player.id)
      end
     end
    end
  end,
}

fa__xuchu:addSkill(fa__xiechan)

Fk:loadTranslationTable{
  ["fa__xuchu"] = "许褚", 
  ["#fa__xuchu"] = "虎臂当骑",
  ["cv:fa__xuchu"] = "天外之音",
  ["illustrator:fa__xuchu"] = "小新",
  ["designer:fa__xuchu"] = "猪猪",

  ["fa__xiechan"] = "挟缠",
  [":fa__xiechan"] = "你指定或成为【杀】的目标后，你可以展示你与对方的手牌并弃置其中所有【闪】。",
  ["#fa__xiechan-invoke"] = "是否发动 挟缠",
  ["#fa_xiechan-invoke"] = "与对方展示手牌并弃置所有的【闪】",

  ["$fa__xiechan1"] = "肥猪赛大象，只是鼻子短！",
  ["$fa__xiechan2"] = "许攸匹夫说曹公坏话？做成人彘丢入猪圈！",
  ["~fa__xuchu"] = "真想再吃一口猪肉啊！",
}

local fa__guanyu = General(extension, "fa__guanyu", "shu", 4,4)

local fa__zhenting = fk.CreateViewAsSkill{
  name = "fa__zhenting",
  anim_type = "offensive",
  pattern = "slash,drowning",
  prompt = "#fa__zhenting",
  card_filter = function(self, to_select, selected)
    if #selected == 1 then return false end
      local _c = Fk:getCardById(to_select)
      local c
      local slash = Fk:cloneCard("slash")
      if _c.color == Card.Red then
        c = Fk:cloneCard("fire__slash")
      elseif _c.color == Card.Black then
        c = Fk:cloneCard("sa__drowning")
      else
        return false
        end
        return (Fk.currentResponsePattern == nil and Self:canUse(c) and Self:canUse(slash))
  end,
  view_as = function(self, cards)
    if #cards ~= 1 then return end
    local _c = Fk:getCardById(cards[1])
    local c
    if _c.color == Card.Red then
        c = Fk:cloneCard("fire__slash")
      elseif _c.color == Card.Black then
        c = Fk:cloneCard("sa__drowning")
    end
    c.skillName = self.name
    c:addSubcard(cards[1])
    return c
  end,
}
local fa__zhenting_trigger = fk.CreateTriggerSkill{
  name = "#fa__zhenting_trigger",
  mute = true,
  events = {fk.CardUseFinished},
  can_trigger = function(self, event, target, player, data)
    return target == player and target.phase == Player.Play
    and data.card and table.contains(data.card.skillNames, "fa__zhenting") and data.card.trueName ~= "fire__slash"
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:notifySkillInvoked(player, self.name, "#fa__zhenting_trigger")
    room:addPlayerMark(player, "fa__zhenting_minus1-turn")
end,
}
local fa__zhenting_targetmod = fk.CreateTargetModSkill{
  name = "#fa__zhenting_targetmod",
  bypass_distances =  function(self, player, skill, card, to)
    return table.contains(card.skillNames, fa__zhenting.name)
  end,
  residue_func = function(self, player, skill, scope)
    if skill.trueName == "slash_skill" and scope == Player.HistoryPhase then
      local n = 0
      local m = player:getMark("fa__zhenting_minus1-turn")
      if m > 0 then
        n = n - m
      end
      return n
    end
  end,
}

fa__zhenting:addRelatedSkill(fa__zhenting_trigger)
fa__zhenting:addRelatedSkill(fa__zhenting_targetmod)
fa__guanyu:addSkill(fa__zhenting)

Fk:loadTranslationTable{
  ["fa__guanyu"] = "关羽", 
  ["#fa__guanyu"] = "义刃薄天",
  ["cv:fa__guanyu"] = "天外之音",
  ["illustrator:fa__guanyu"] = "鬼画府",
  ["designer:fa__guanyu"] = "楚萱",

  ["fa__zhenting"] = "震霆",
  [":fa__zhenting"] = "你可以将一张红色牌当无距离限制的火【杀】，或将一张黑色牌当计入【杀】次数限制的【水淹七军】使用。" ..
  "<br/><font color='blue'><b>注</b>：代码实现的方法为：以使用本技能转化的【水淹七军】后减少一次本回合使用【杀】次数。</font>" ..
  "<br/><font color='blue'>请不要反馈类似“使用下一张牌无次数限制”后以本技能转化【水淹七军】被占用使用【杀】次数的问题。</font>",
  ["#fa__zhenting"] = "发动 震霆， 可将红色牌当火【杀】，或将黑色牌当【水淹七军】使用。",
  ["fa__zhenting_minus"] = "震霆 转化的【水淹七军】已占用本回合 怀异关羽 使用【杀】的限制次数",
  ["#fa__zhenting_trigger"] = "震霆",

  ["$fa__zhenting1"] = "我自山峰而下，犹未见来人！",
  ["$fa__zhenting2"] = "千军斩将而回，于某又有何难？",
  ["$fa__zhenting3"] = "关某的大刀不斩老幼，且试吾匕首利否！",
  ["$fa__zhenting4"] = "既饮柳皮醋水，恕难再饮此酒。",
  ["~fa__guanyu"] = "身首凄凄葬异处，桃园切切闻哀哭...",
}

local fa__yujin = General(extension, "fa__yujin", "wei", 4,4)

local fa__zhiyan = fk.CreateTriggerSkill{
  name = "fa__zhiyan",
  anim_type = "support",
  events = {fk.TurnStart},
  can_trigger = function(self, event, target, player, data)
    return target ~= player and player:hasSkill(self) and not target.dead
    and (player:getMark("fa__zhiyan1-round") == 0 or player:getMark("fa__zhiyan2-round") == 0)
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local choices = {}
    for i = 1, 3, 1 do
      local mark = "fa__zhiyan"..tostring(i).."-round"
      if player:getMark(mark) == 0 then
        table.insert(choices, mark)
        if player.dead or #choices == 0 then break end
      end
    end
    local choice = player.room:askForChoice(player, choices, "fa__zhiyan", "#fa__zhiyan-choice::"..target.id)
    if choice == "fa__zhiyan1-round" then
       room:setPlayerMark(player, choice, 1)
       room:drawCards(target, 2, self.name)
       room:setPlayerMark(target, "@@fa__zhiyan1-turn", 1)
    elseif choice == "fa__zhiyan2-round" then
       room:setPlayerMark(player, choice, 1)
       room:drawCards(target, 2, self.name)
       room:setPlayerMark(target, "@@fa__zhiyan2-turn", 1)
    elseif choice == "fa__zhiyan3-round" then
        return false
      end
  end,
}
local fa__zhiyan_punish = fk.CreateTriggerSkill{
  name = "#fa__zhiyan_punish",
  events = {fk.TurnEnd},
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(self) then return false end
    return target:getMark("@@fa__zhiyan1-turn") > 0 or target:getMark("@@fa__zhiyan2-turn") > 0
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if (#player.room.logic:getActualDamageEvents(1, function(e) return e.data[1].from == target end, Player.HistoryTurn) > 0
    and target:getMark("@@fa__zhiyan2-turn") > 0)
    or (#player.room.logic:getActualDamageEvents(1, function(e) return e.data[1].from == target end, Player.HistoryTurn) == 0
    and target:getMark("@@fa__zhiyan1-turn") > 0)  then
      room:damage {
        from = player, to = target,
        damage = 1, skillName = self.name
      }
    else return false end
  end,
}

fa__zhiyan:addRelatedSkill(fa__zhiyan_punish)
fa__yujin:addSkill(fa__zhiyan)

Fk:loadTranslationTable{
  ["fa__yujin"] = "于禁", 
  ["#fa__yujin"] = "肃凛千军",
  ["cv:fa__yujin"] = "天外之音",
  ["illustrator:fa__yujin"] = "凡果",
  ["designer:fa__yujin"] = "伶伶",

  ["fa__zhiyan"] = "治严",
  [":fa__zhiyan"] = "每轮各限一次，其他角色回合开始时，你可以令其摸两张牌，强令其直至本回合结束时:1.造成过伤害; 2.不造成伤害。失败:你对其造成1点伤害。",
  ["#fa__zhiyan-choice"] = "治严：可以令 %dest 角色摸两张牌并向其发布任务（出击/坚守）",
  ["fa__zhiyan1-round"] = "强令 本回合造成伤害",
  ["fa__zhiyan2-round"] = "强令 本回合不造成伤害",
  ["fa__zhiyan3-round"] = "本回合自由活动（取消）",
  ["@@fa__zhiyan1-turn"] = "治严 令行",
  ["@@fa__zhiyan2-turn"] = "治严 禁止",
  ["#fa__zhiyan_punish"] = "治严",

  ["$fa__zhiyan1"] = "（造成）传令三军，兵发樊城，救援大将军！",
  ["$fa__zhiyan2"] = "（不造成）传令三军，闭目塞耳，坚守不战！",
  ["$fa__zhiyan3"] = "（处罚）违我军令，死罪苟免，活罪难逃！",
  ["$fa__zhiyan4"] = "（未处罚）军纪肃然，虽亚夫之细柳营，能复比耶？",
  ["~fa__yujin"] = "悔教昌豨做石阶，逢此巨变，终难全晚节...",
}

local fa__yanliangwenchou = General(extension, "fa__yanliangwenchou", "qun", 4,4)

local fa__shuangxiong = fk.CreateViewAsSkill{
  name = "fa__shuangxiong",
  anim_type = "offensive",
  frequency = Skill.Compulsory,
  enabled_at_play = function(self, player)
    return player:getMark("@@fa__shuangxiong_yanliang") == 1
  end,
  card_filter = function(self, to_select, selected)
    if #selected ~= 0 then return false end
    local _c = Fk:getCardById(to_select)
    local c
    if _c.name == "jink" then
      c = Fk:cloneCard("duel")
    else
      return false
    end
    return (Fk.currentResponsePattern == nil and c.skill:canUse(Self, c))
  end,
  view_as = function(self, cards)
    if #cards ~= 1 then
      return nil
    end
    local _c = Fk:getCardById(cards[1])
    local c
    if _c.name == "jink" then
      c = Fk:cloneCard("duel")
    end
    c.skillName = self.name
    c:addSubcard(cards[1])
    return c
  end,
}
local fa__shuangxiong_mark = fk.CreateTriggerSkill{
  name = "#fa__shuangxiong_mark",
  frequency = Skill.Compulsory,
  events = {fk.CardUsing},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self)
    and player:getMark("@@fa__shuangxiong_yanliang") == 0 and player:getMark("@@fa__shuangxiong_wenchou") == 0
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if Fk:translate(data.card.trueName, "zh_CN"):len() % 2 ~= 0 then
    room:setPlayerMark(player, "@@fa__shuangxiong_yanliang", 1)
elseif Fk:translate(data.card.trueName, "zh_CN"):len() % 2 == 0 then
    room:setPlayerMark(player, "@@fa__shuangxiong_wenchou", 1)
    end
  end,
}
local fa__shuangxiong_targetmod = fk.CreateTargetModSkill{
  name = "#fa__shuangxiong_targetmod",
  extra_target_func = function(self, player, skill)
    if player:getMark("@@fa__shuangxiong_wenchou") > 0 then
     if skill.trueName == "fire__slash_skill" or skill.trueName == "fire_attack_skill" or skill.trueName == "thunder__slash_skill" 
     or skill.trueName == "ice__slash_skill" or skill.trueName == "sa__drowning_skill" or skill.trueName == "drowning_skill"
     or skill.trueName == "ty__drowning_skill"  then --穷举范围：军争+应变+势备+三服衍生，闪电，浮雷，火烧连营是否能够加目标存疑（能我也不想加）
       return 1
     end
   end
  end,
}

fa__shuangxiong:addRelatedSkill(fa__shuangxiong_targetmod)
fa__shuangxiong:addRelatedSkill(fa__shuangxiong_mark)
fa__yanliangwenchou:addSkill(fa__shuangxiong)

Fk:loadTranslationTable{
  ["fa__yanliangwenchou"] = "颜良文丑", 
  ["#fa__yanliangwenchou"] = "川涌岳撼",
  ["cv:fa__yanliangwenchou"] = "天外之音",
  ["illustrator:fa__yanliangwenchou"] = "利维坦·车",
  ["designer:fa__yanliangwenchou"] = "幽蝶",

  ["fa__shuangxiong"] = "双雄",
  [":fa__shuangxiong"] = "锁定技，若你本局首次使用的牌牌名字数为：奇数，你可以将【闪】当【决斗】使用；偶数，你使用属性类伤害牌目标上限+1。" ..
  "<br/><font color='blue'><b>注</b>：属性类伤害牌穷举范围：军争+应变+势备+三服衍生（除【闪电】【浮雷】【火烧连营】）。</font>",
  ["@@fa__shuangxiong_yanliang"] = "双雄效果 颜良",
  ["@@fa__shuangxiong_wenchou"] = "双雄效果 文丑",
  ["#fa__shuangxiong_mark"] = "双雄",

  ["$fa__shuangxiong1"] = "（颜良）闪当决斗，震巍巍高楼，谁敢品头？",
  ["$fa__shuangxiong2"] = "（文丑）雷霆雨露，播泱泱汉土，谁敢论足？",
  ["~fa__yanliangwenchou"] = "淫威滥施，骄横自恃，二人乱军取死...",
}

local fa__zhaoyun = General(extension, "fa__zhaoyun", "shu", 2,2)

local fa__juejing = fk.CreateTriggerSkill{
  name = "fa__juejing",
  anim_type = "drawcard",
  frequency = Skill.Compulsory,
  events = {fk.EventPhaseStart},
  mute = true,
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and target.phase == Player.Start and player:getHandcardNum() ~= player:getLostHp() + 2
  end,
  on_use = function(self, event, target, player, data)
    player:broadcastSkillInvoke(self.name)
    local room = player.room
    local n = player:getHandcardNum() - player:getLostHp() - 2
    if n < 0 then
      player:drawCards(-n, self.name)
    else
      room:askForDiscard(player, n, n, false, self.name, false)
    end
  end,
}
local fa__juejing_maxcards = fk.CreateMaxCardsSkill{
  name = "#fa__juejing_maxcards",
  correct_func = function(self, player)
    if player:hasSkill(fa__juejing.name) then
      return 2
    end
  end
}
local fa__chongzhen = fk.CreateTriggerSkill{
  name = "fa__chongzhen",
  anim_type = "defensive",
  events = {fk.DamageInflicted},
  frequency = Skill.Compulsory,
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and (data.card == nil or data.card.trueName ~= "duel")
    and data.from ~= nil and not (data.from.dead or data.from == player)
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    return true and room:useVirtualCard("duel", nil, player, data.from, self.name, true)
  end,
}
fa__juejing:addRelatedSkill(fa__juejing_maxcards)
fa__zhaoyun:addSkill(fa__juejing)
fa__zhaoyun:addSkill(fa__chongzhen)

Fk:loadTranslationTable{
  ["fa__zhaoyun"] = "赵云", 
  ["#fa__zhaoyun"] = "神击将胆",
  ["cv:fa__zhaoyun"] = "天外之音",
  ["illustrator:fa__zhaoyun"] = "匠人绘",
  ["designer:fa__zhaoyun"] = "猪猪",

  ["fa__juejing"] = "绝境",
  [":fa__juejing"] = "锁定技，你的手牌上限+2X。准备阶段，你将手牌数调整至X+2张。X为你已损失的体力值。",
  ["fa__chongzhen"] = "冲阵",
  [":fa__chongzhen"] = "锁定技，你受到其他角色造成的伤害时，若不为【决斗】造成的伤害，你防止之，然后视为对伤害来源使用【决斗】。",

  ["$fa__juejing1"] = "再晚一点跟来，就赶不上渡河了！",
  ["$fa__juejing2"] = "天王老子来了，也得把阿斗带走！",
  ["$fa__chongzhen1"] = "龙啊，吞噬我的敌人吧！",
  ["$fa__chongzhen2"] = "尝尝神龙之剑吧！",
  ["~fa__zhaoyun"] = "伯约，你是龙的话，也好...",
}

return extension